﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Factory
{
    public class FactorySND : ProtocolMessageFactory
    {
        public enum ParameterEnum : int
        {
            Type = 0,
            Identifier = 1,
            StartPosition = 2,
            BytesToSend = 3,
        }

        public enum SNDTypesEnum : int
        {
            File = 0,
            List = 1
        }

        public override ProtocolMessage Create(ProtocolMessage.MessageTypeEnum messageType, Dictionary<int, object> parameters)
        {
            ProtocolMessage proto = new ProtocolMessage(messageType, ProtocolFeatureBASE.FeatureActions.ActionSND);

            int type = (int)parameters[(int)ParameterEnum.Type];
            string typeStr = string.Empty;
            if (GetTypeString(type, out typeStr))
            {
                proto.Parameters.Add(new ProtocolParameter(typeStr));
            }
            else
            {
                // call extension handlers
                // if (extensionResult != null)
                // {
                //  typeStr = extensionResult.ToString();
                //  if (typeStr != string.Empty)
                //  {
                //      proto.GETParameterOptions.Add(new ProtocolParameter(typeStr));
                //  }
                // }
            }

            // "Identifier must come from the namespace of the current session hash", so check that it does...
            string identifier = parameters[(int)ParameterEnum.Identifier].ToString();
            proto.Parameters.Add(new ProtocolParameter(identifier));

            if (parameters.ContainsKey((int)ParameterEnum.StartPosition))
            {
                proto.Parameters.Add(new ProtocolParameter(parameters[(int)ParameterEnum.StartPosition].ToString()));
            }
            else
            {
                proto.Parameters.Add(new ProtocolParameter("0"));   // for example with a 'list' type, this is always zero
            }

            if (parameters.ContainsKey((int)ParameterEnum.BytesToSend))
            {
                proto.Parameters.Add(new ProtocolParameter(parameters[(int)ParameterEnum.BytesToSend].ToString()));
            }

            return proto;
        }

        private bool GetTypeString(int type, out string typeString)
        {
            if (Enum.IsDefined(typeof(SNDTypesEnum), type))
            {
                switch ((SNDTypesEnum)type)
                {
                    case SNDTypesEnum.File:
                        typeString = "file";
                        return true;
                    case SNDTypesEnum.List:
                        typeString = "list";
                        return true;
                    default:
                        break;
                }
            }
            typeString = string.Empty;
            return false;
        }
    }
}
